class BorrowBooksController < ApplicationController
  before_filter :authenticate_user!
  # GET /borrow_books
  # GET /borrow_books.json
  def index
    @requests = nil
    if session[:user].role == "admin"
      @requests = Request.all
    else
      @requests = Request.find(:all, :conditions => ['user_id = ?', session[:user].id ])
    end
    
    @books=@requests.map{|x| x.book}
    @users=@requests.map{|x| x.user}
    
    @borrow_books = BorrowBook.all
    @books1=@borrow_books.map{|x| x.book}
    @users1=@borrow_books.map{|x| x.user}
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @borrow_books }
    end
  end

  # GET /borrow_books/1
  # GET /borrow_books/1.json
  def show
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
      @borrow_book = BorrowBook.find(params[:id])
      respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @borrow_book }
      end
    end
  end

  # GET /borrow_books/new
  # GET /borrow_books/new.json
  def new
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
      @borrow_book = BorrowBook.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @borrow_book }
      end
    end
  end

  # GET /borrow_books/1/edit
  def edit
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
      @borrow_book = BorrowBook.find(params[:id])
    end
  end

  # POST /borrow_books
  # POST /borrow_books.json
  def create
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
      @borrow_book = BorrowBook.new(params[:borrow_book])

      respond_to do |format|
        if @borrow_book.save
          format.html { redirect_to @borrow_book, notice: 'Borrow book was successfully created.' }
          format.json { render json: @borrow_book, status: :created, location: @borrow_book }
        else
          format.html { render action: "new" }
          format.json { render json: @borrow_book.errors, status: :unprocessable_entity }
        end
      end
    end
  end

  # PUT /borrow_books/1
  # PUT /borrow_books/1.json
  def update
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
      @borrow_book = BorrowBook.find(params[:id])

      respond_to do |format|
        if @borrow_book.update_attributes(params[:borrow_book])
          format.html { redirect_to @borrow_book, notice: 'Borrow book was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render action: "edit" }
          format.json { render json: @borrow_book.errors, status: :unprocessable_entity }
        end
      end
    end
  end

  # DELETE /borrow_books/1
  # DELETE /borrow_books/1.json
  def destroy
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
      @borrow_book = BorrowBook.find(params[:id])
      @borrow_book.destroy

      respond_to do |format|
        format.html { redirect_to borrow_books_url }
        format.json { head :no_content }
      end
    end
  end

end
